import requests
import json

#1.新建会话，得到conversation_id

url = "https://qianfan.baidubce.com/v2/app/conversation"

payload = json.dumps({
    "app_id": "b67730f6-d6df-4178-9529-1f4dcba54af4"
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-YfvbJOvKUy3YuEOlVtPWp/bec969acfd1564e2331e4504af8ed99febfb540e'
}

response = requests.request("POST", url, headers=headers, data=payload)


data = json.loads(response.text)
conversation_id = data["conversation_id"]

#2.发送请求，得到AppBuilder的结果

url = "https://qianfan.baidubce.com/v2/app/conversation/runs"

payload = json.dumps({
    "app_id": "b67730f6-d6df-4178-9529-1f4dcba54af4",
    "query": input("请输入问题："),
    "stream": False,
    "conversation_id": conversation_id
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-YfvbJOvKUy3YuEOlVtPWp/bec969acfd1564e2331e4504af8ed99febfb540e'
}

response = requests.request("POST", url, headers=headers, data=payload)

#print(response.text)


data = json.loads(response.text)
conversation_id = data["answer"]


print(conversation_id)

